package multimedia;

import java.rmi.RemoteException;

import javax.microedition.lcdui.Displayable;

import midlet.VadeMecumMIDlet;
import ui.VadeMecumImageViewer;
import util.Base64Util;
import webservice.VadeMecumMultimediaServicePortType_Stub;

public class GetFileThread implements Runnable {

	private VadeMecumMIDlet midlet;
	
	private Displayable previous;
	
	private Integer fileId;
	
	private VadeMecumMultimediaServicePortType_Stub stub;
	
	public GetFileThread(VadeMecumMIDlet midlet, Displayable previous, Integer fileId) {
		this.midlet = midlet;
		this.previous = previous;
		this.fileId = fileId;
	}
	
	
	public void run() {
		System.out.println("get file ID = " + fileId);
		stub = new VadeMecumMultimediaServicePortType_Stub();
		try {
			String fileBytesAsString = stub.getFile(fileId);
			VadeMecumImageViewer viewer = new VadeMecumImageViewer(this.midlet, Base64Util.decode(fileBytesAsString));
			viewer.setCommandListener(this.midlet);
			this.midlet.display.setCurrent(viewer);
		} catch (RemoteException re) {
			this.midlet.error(re);
		}	
	}

}
